# _*_ coding: utf-8 _*_
import os
import sys
from time import strftime, mktime, localtime, time, strptime
from typing import List, AnyStr

__ResPathCache = {}


def now() -> str:
    """
    获取当前时间
    :return:  返回的时间格式为: 2019-07-04 11:11:42
    """
    return strftime("%Y-%m-%d %H:%M:%S", localtime())


def timestamp() -> int:
    """
    获取当前时间戳
    :return:
    """
    return int(time())


def timeStrToTimestamp(timeStr: str) -> int:
    """
    时间字符串转换为时间戳
    :param timeStr: 时间字符串
    :return: 时间戳
    """
    return int(mktime(strptime(timeStr, "%Y-%m-%d %H:%M:%S")))


def pyInstallerResPath(path: str) -> str:
    if path not in __ResPathCache:
        __ResPathCache[path] = os.path.join(getattr(sys, '_MEIPASS', os.getcwd()), path.replace("/", os.sep))
    return __ResPathCache[path]


def readLines(file: str, encoding: str = "utf-8") -> List[AnyStr]:
    """
    从文件读取文本行
    :param file: 文件
    :param encoding: 编码
    :return: 读取到的行
    """
    with open(file, mode="r", encoding=encoding) as f:
        return f.readlines()


def readText(file: str, encoding: str = "utf-8") -> str:
    """
    从文件读取文本
    :param file:文件
    :param encoding:编码
    :return: 文本
    """
    return "".join(readLines(file, encoding=encoding))


def writeText(file: str, text: AnyStr, encoding: str = "utf-8") -> int:
    """
    写文本到文件
    :param file: 文件
    :param text: 文本
    :param encoding: 编码
    :return: 写入成功的字节
    """
    with open(file, mode="w", encoding=encoding) as f:
        return f.write(text)


def writeLines(file: str, lines: List[AnyStr], encoding: str = "utf-8"):
    """
    写文本行到文件
    :param file: 文件
    :param lines: 文本行
    :param encoding: 编码
    :return: 写入成功的字节
    """
    with open(file, mode="w", encoding=encoding) as f:
        f.writelines(lines)
